//
//  JYHomeViewController.swift
//  JYCalculate
//
//  Created by 何治军 on 2018/10/26.
//  Copyright © 2018 何治军. All rights reserved.
//

import UIKit
import Masonry

class JYHomeViewController: JYBaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.hiddenLeftBtn = true
        setUpUI()
        collectionView.dateArray = [["title":Localized("function-currency"),"className":"JYCalculate.JYCurrencyViewController"],["title":Localized("function-house-loan"),"className":"JYCalculate.JYHouseLoanViewController"],["title":Localized("function-personal-tax"),"className":"JYCalculate.JYPersonalTaxViewController"],["title":"a"]]
    }
    
    override func setUpUI(){
        view.addSubview(collectionView)
        
        collectionView.selecteTheItem = {[weak self] (indexPath) in
            if let className = self?.collectionView.dateArray[indexPath.item]["className"] {
                if let vcClass = NSClassFromString(className) as? NSObject.Type {
                    let v = vcClass.init() as! UIViewController
                    v.hidesBottomBarWhenPushed = true
                    self?.navigationController?.pushViewController(v , animated: true)
                }
            }
        }
        setTheLayout()
    }
    
    override func setTheLayout(){
        collectionView.mas_updateConstraints { (make) in
            make?.edges.mas_equalTo()(self.view)
        }
    }
    
    var collectionView:JYHomeCollectionView = {
        let v = JYHomeCollectionView()
        v.backgroundColor = JYDetailGrayColor
        return v
    }()

}

class JYHomeCollectionView: UICollectionView,UICollectionViewDataSource,UICollectionViewDelegate {
    private let reuseIdentifier = "JYHomeCollectionViewCell"

    var dateArray:[[String:String]] = []{
        didSet{
            reloadData()
        }
    }
    //点击item的回调
    var selecteTheItem = {(indexPath:IndexPath) -> (Void) in}
    
    convenience init() {
        self.init(frame: CGRect(), collectionViewLayout: UICollectionViewFlowLayout())
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        register(JYHomeCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        dataSource = self
        delegate = self
        isPagingEnabled = true
        bounces = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        let margin:CGFloat = 5
        layout.itemSize = CGSize(width: (self.frame.width - margin * 2) / 3 , height: (self.frame.height - margin * 2) / 3)
        layout.minimumInteritemSpacing = margin
        layout.minimumLineSpacing = margin
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dateArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView .dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! JYHomeCollectionViewCell
        cell.titleLab.text = dateArray[indexPath.item]["title"];
        cell.backgroundColor = JYNavigationBarColor
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selecteTheItem(indexPath)
    }
    
}

class JYHomeCollectionViewCell: UICollectionViewCell {
    
    /// 标题
    var titleLab:UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 17)
        lab.textAlignment = .center
        return lab
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setUpUI() {
        contentView.addSubview(titleLab)
        self.layer.cornerRadius = 4
    }
    
    override class var requiresConstraintBasedLayout :Bool {
        get{
            return true
        }
    }
    
    override func updateConstraints() {
        super.updateConstraints()
        titleLab.mas_updateConstraints { (make) in
            make?.edges.mas_equalTo()(self)
        }
    }

}
